package com.zhulin.android.edoctor.parsehandler;

import java.io.ByteArrayOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.zhulin.android.edoctor.jsonobject.OUTGetCaseList;
import com.zhulin.android.edoctor.jsonobject.OUTGetCaseList.GetCaseListModel;
import com.zhulin.android.edoctor.net.ParseHandler;

public class GetCaseListParseHandler implements ParseHandler{

	@SuppressWarnings("finally")
	@Override
	public Object handle(ByteArrayOutputStream str) {
		
		OUTGetCaseList outGetCaseList=new OUTGetCaseList();
		
		String ret = new String(str.toByteArray()).trim();
		try {
			JSONObject jsonObject=new JSONObject(ret);
			
			
			int result=jsonObject.getInt(Result);
			if(result==1){
				//SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				outGetCaseList.setResult(result);
				JSONArray jsonArray= jsonObject.getJSONArray("CaseList");
				for(int i=0;i<jsonArray.length();i++){
					JSONObject json= jsonArray.getJSONObject(i);
					GetCaseListModel caseListModel=new GetCaseListModel();
					caseListModel.setCaseID(json.getString("CaseID"));
					caseListModel.setCategory(json.getInt("Category"));
					caseListModel.setCaseTitle(json.getString("CaseTitle"));
					caseListModel.setPicture1(json.getString("Picture1"));
					caseListModel.setPicture2(json.getString("Picture2"));
					caseListModel.setLaudCount(json.getInt("LaudCount"));
					caseListModel.setCommentCount(json.getInt("CommentCount"));
					
					String strDate=json.getString("UploadDate");
					//Date uploadDate=new Date(formatter.format(strDate));
					caseListModel.setUploadDate(strDate);
					
					outGetCaseList.getCaseList().add(caseListModel);
				}
			}else{
				outGetCaseList.setDescription(jsonObject.getString(Descption));
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		finally{
			return outGetCaseList;
		}
	}
}
